package com.platform.modules.wallet.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.platform.common.constant.ApiConstant;
import com.platform.common.shiro.ShiroUtils;
import com.platform.common.version.ApiVersion;
import com.platform.common.version.VersionEnum;
import com.platform.common.web.domain.AjaxResult;
import com.platform.modules.chat.domain.ChatGroup;
import com.platform.modules.chat.domain.ChatUser;
import com.platform.modules.chat.enums.MsgStatusEnum;
import com.platform.modules.chat.service.ChatGroupService;
import com.platform.modules.chat.service.ChatUserService;
import com.platform.modules.push.enums.PushMsgTypeEnum;
import com.platform.modules.push.service.ChatPushService;
import com.platform.modules.push.vo.PushParamVo;
import com.platform.modules.wallet.domain.WalletRedPacket;
import com.platform.modules.wallet.domain.WalletRedPacketSub;
import com.platform.modules.wallet.enums.RedPacketTypeEnum;
import com.platform.modules.wallet.service.WalletRedPacketService;
import com.platform.modules.wallet.service.WalletRedPacketSubService;
import com.platform.modules.wallet.service.WalletService;
import com.platform.modules.wallet.vo.RedPacketVo02;
import com.platform.modules.wallet.vo.RedPacketVo03;
import com.platform.modules.wallet.vo.RedPacketVo04;
import com.platform.modules.wallet.vo.UserVo;
import com.tencentcloudapi.cms.v20190321.models.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 钱包-红包
 */
@RestController
@Slf4j
@RequestMapping("/wallet")
public class RedPacketController {

    @Resource
    private ChatUserService chatUserService;
    @Resource
    private WalletRedPacketService walletRedPacketService;

    /**
     * 查询红包信息
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getRedPacket/{queryId}")
    public AjaxResult getRedPacket(@PathVariable String queryId) {
        WalletRedPacket redPacket = walletRedPacketService.getByQueryId(queryId);
        return AjaxResult.success(walletRedPacketService.findById(redPacket.getId()));
    }

    /**
     * 查询是否抢过红包
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/hasRedPacket/{queryId}")
    public AjaxResult hasRedPacket(@PathVariable String queryId) {

        Long userId = ShiroUtils.getUserId();

        WalletRedPacket redPacket = walletRedPacketService.getByQueryId(queryId);

        return AjaxResult.success(walletRedPacketService.findToIdByUserId(redPacket.getId(), userId));
    }

    /**
     * 查询是否还有可抢红包
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/haveRedPacket/{queryId}")
    public AjaxResult haveRedPacket(@PathVariable String queryId) {

        WalletRedPacket redPacket = walletRedPacketService.getByQueryId(queryId);
        List<WalletRedPacketSub> walletRedPacketSubs = walletRedPacketService.querySubInitList(redPacket.getId());

        return AjaxResult.success(walletRedPacketSubs.size()>0);
    }

    /**
     * 开红包
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/openRedPacket")
    public AjaxResult openRedPacket(@Validated @RequestBody RedPacketVo02 vo) {

        Long userId = ShiroUtils.getUserId();
        boolean b = walletRedPacketService.openRedPacket(vo.getId(), userId);

        return AjaxResult.success(b);
    }

    /**
     * 查询已接收的红包列表
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/queryRedPacket/{queryId}")
    public AjaxResult queryRedPacket(@PathVariable String queryId) {

        WalletRedPacket redPacket = walletRedPacketService.getByQueryId(queryId);

        RedPacketVo04 redPacketVo04 = Convert.convert(RedPacketVo04.class, redPacket);

        List<RedPacketVo03> list = new ArrayList<>();

        List<WalletRedPacketSub> walletRedPacketSubs = walletRedPacketService.querySubSuccessList(redPacket.getId());
        for (WalletRedPacketSub walletRedPacketSub : walletRedPacketSubs) {
            RedPacketVo03 convert = Convert.convert(RedPacketVo03.class, walletRedPacketSub);
            ChatUser to = chatUserService.findById(convert.getToId());
            UserVo toVO = Convert.convert(UserVo.class, to);
            convert.setTo(toVO);
            list.add(convert);
        }

        redPacketVo04.setSubVoList(list);

        return AjaxResult.success(redPacketVo04);
    }
}
